package com.qidi.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qidi.ssh.bean.Customer;
import com.qidi.ssh.service.CustomerService;
import com.qidi.ssh.utils.PageBean;
import com.qidi.ssh.utils.UploadUtils;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-10-16-21:44
 */
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
    private Customer customer=new Customer();
    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @Override
    public Customer getModel() {
        return customer;
    }

    public String addpage(){
        return "page";
    }

    private String uploadFileName;
    private File upload;
    private String uploadContextType;

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public void setUploadContextType(String uploadContextType) {
        this.uploadContextType = uploadContextType;
    }

    public String save() throws IOException {
        if (upload!=null){
            String path="e:/upload";
            String fileName = UploadUtils.getUuidFileName(uploadFileName);
            String realPath = UploadUtils.getPath(fileName);

            String url=path+realPath;
            File file = new File(url);
            if (!file.exists()){
                file.mkdirs();
            }
            String FinalfileName=url + File.separator + fileName;
            //文件上传
            File finalfile = new File(FinalfileName);
            FileUtils.copyFile(upload,finalfile);

            customer.setCust_image(FinalfileName);
        }
        customerService.saveCustomer(customer);
//        return "save";
        return NONE;
    }

    private Integer pageNo=1;
    private Integer pageSize=3;

    public void setPageSize(Integer pageSize) {
        if (pageSize==null){
            pageSize=3;
        }
        this.pageSize = pageSize;
    }

    public void setPageNo(Integer pageNo) {
        if (pageNo==null){
            pageNo=1;
        }
        this.pageNo = pageNo;
    }



    public String list(){

        DetachedCriteria detachedCriteria=DetachedCriteria.forClass(Customer.class);

        if (customer.getCust_name()!=null ){
            detachedCriteria.add(Restrictions.like("cust_name","%"+customer.getCust_name()+"%"));
        }
        if (customer.getBaseDictSource()!=null)
        {
            if (customer.getBaseDictSource().getDict_id()!=null && !"".equals(customer.getBaseDictSource().getDict_id())){
                detachedCriteria.add(Restrictions.eq("baseDictSource.dict_id",customer.getBaseDictSource().getDict_id()));
            }
        }
        if (customer.getBaseDictLevel()!=null)
        {
            if (customer.getBaseDictLevel().getDict_id()!=null&&!"".equals(customer.getBaseDictLevel().getDict_id())){
                detachedCriteria.add(Restrictions.eq("baseDictLevel.dict_id",customer.getBaseDictLevel().getDict_id()));
            }
        }
        if (customer.getBaseDictIndustry()!=null)
        {
            if (customer.getBaseDictIndustry().getDict_id()!=null&&!"".equals(customer.getBaseDictIndustry().getDict_id())){
                detachedCriteria.add(Restrictions.eq("baseDictIndustry.dict_id",customer.getBaseDictIndustry().getDict_id()));
            }
        }

        PageBean<Customer> customers=customerService.ListByPage(detachedCriteria,pageSize,pageNo);
        ActionContext.getContext().getValueStack().push(customers);
       // ActionContext.getContext().getValueStack().push(customer);
        return "list";
    }

    public String delete(){
        customerService.deleteCustomer(customer.getCust_id());
        return "deleteSuccess";
    }
    public String edit(){
        Customer customer = customerService.edit(this.customer.getCust_id());
        System.out.println(customer);
        ActionContext.getContext().getValueStack().push(customer);
        return "edit";
    }
    public String update() throws IOException {
        if (upload!=null){
            String image = customer.getCust_image();
            if (image!=null || "".equals(image)){
                File file = new File(image);
                if (file!=null){
                    file.delete();
                }
            }

            //文件上传:
//设置文件上传的路径
            String path="e:/upload";
            String fileName = UploadUtils.getUuidFileName(uploadFileName);
            String realPath = UploadUtils.getPath(fileName);

            String url=path+realPath;
            File file = new File(url);
            if (!file.exists()){
                file.mkdirs();
            }
            String FinalfileName=url + File.separator + fileName;
            //文件上传
            File finalfile = new File(FinalfileName);
            FileUtils.copyFile(upload,finalfile);

            customer.setCust_image(FinalfileName);
        }
        customerService.UpdateCustomer(customer);
        return "update";
    }

    public String findAllCustomer() throws IOException {
        List<Customer> list = customerService.findAlls();
        System.out.println(list);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"linkmens","baseDictSource","baseDictIndustry","baseDictLevel"});
        JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
        ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
        ServletActionContext.getResponse().getWriter().write(jsonArray.toString());
        return NONE;
    }
}
